//
//  ViewController.m
//  GifImageView
//
//  Created by Almas on 16/8/26.
//  Copyright © 2016年 Ali. All rights reserved.
//

#import "ViewController.h"
#import "FLAnimatedImageView.h"
#import "FLAnimatedImage.h"

//#import "UIImageView+WebCache.h"
//#import "UIView+SetRect.h"
//#import "GCD.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.layer.masksToBounds = YES;
    
    [self setUpLocalGif];//本地播放
    [self setUpGifWithURL];//网络下载播放
    
    
    
    
    
}

-(void)setUpLocalGif{
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"myGif" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];

    FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];
    
    FLAnimatedImageView *gifImageView = [[FLAnimatedImageView alloc] init];
    
    gifImageView.frame           = CGRectMake(0, 0, 320, gifImage.size.height*0.4);
    gifImageView.animatedImage   = gifImage;
    gifImageView.alpha           = 0.f;
    
    [UIView animateWithDuration:1.f animations:^{
        
        gifImageView.alpha = 1.f;
    }];
    [self.view addSubview:gifImageView];
}

-(void)setUpGifWithURL{
    
//    FLAnimatedImageView *gifImageView = [[FLAnimatedImageView alloc] init];
//    gifImageView.frame                = CGRectMake(0.0, 0.0, 100.0, 100.0);
//    [self.contentView addSubview:gifImageView];
//    
//    __weak GifPictureController *wself = self;
//    NSString *imagePath                = @"http://images2015.cnblogs.com/blog/607542/201601/607542-20160123090832343-133952004.gif";
//    NSData   *gifImageData             = [self imageDataFromDiskCacheWithKey:imagePath];
//    
//    if (gifImageData) {
//        
//        [wself animatedImageView:gifImageView data:gifImageData];
//        
//    } else {
//        
//        NSURL *url = [NSURL URLWithString:imagePath];
//        [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:url
//                                                              options:0
//                                                             progress:nil
//                                                            completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
//                                                                
//                                                                [[[SDWebImageManager sharedManager] imageCache] storeImage:image
//                                                                                                      recalculateFromImage:NO
//                                                                                                                 imageData:data
//                                                                                                                    forKey:url.absoluteString
//                                                                                                                    toDisk:YES];
//                                                                
//                                                                [[GCDQueue mainQueue] execute:^{
//                                                                    
//                                                                    [wself animatedImageView:gifImageView data:data];
//                                                                }];
//                                                            }];
//    }
//
    
}

//- (void)animatedImageView:(FLAnimatedImageView *)imageView data:(NSData *)data {
//    
//    FLAnimatedImage *gifImage = [FLAnimatedImage animatedImageWithGIFData:data];
//    imageView.frame           = CGRectMake(0, 0, gifImage.size.width, gifImage.size.height);
//    imageView.center          = self.contentView.middlePoint;
//    imageView.animatedImage   = gifImage;
//    imageView.alpha           = 0.f;
//    
//    [UIView animateWithDuration:1.f animations:^{
//        
//        imageView.alpha = 1.f;
//    }];
//}
//
//- (NSData *)imageDataFromDiskCacheWithKey:(NSString *)key {
//    
//    NSString *path = [[[SDWebImageManager sharedManager] imageCache] defaultCachePathForKey:key];
//    return [NSData dataWithContentsOfFile:path];
//}


@end
